/**
 * Created by liuqingxia on 2016/3/14.
 */

/**
 * 展示所有的dataSource数据
 * @param viewDiv
 */
function showAllDataSourceTreeView(viewDiv) {
    showTreeView(viewDiv, getAllDataSourceTreeData());
}

/**
 * 返回所有的treeData数据
 * @returns {{text: string, state: {expanded: boolean}, tags: string[], nodes: Array}[]}
 */
function getAllDataSourceTreeData() {
    var treeData = [
        {
            text: '<span style="font-size: 13px;font-weight: 600">mysql</span>',
            state: {
                expanded: false
            },
            tags: ['4'],
            nodes: getSourceArray("1", buttons)

        },
        {
            text: '<span style="font-size: 13px;font-weight: 600">drds</span>',
            state: {
                expanded: false
            },
            tags: ['0'],
            nodes: getSourceArray("2", buttons)
        },
        {
            text: '<span style="font-size: 13px;font-weight: 600">oracle</span>',
            state: {
                expanded: false
            },
            tags: ['0'],
            nodes: getSourceArray("3", buttons)
        },
        {
            text: '<span style="font-size: 13px;font-weight: 600">sqlserver</span>',
            state: {
                expanded: false
            },
            tags: ['0'],
            nodes: getSourceArray("4", buttons)
        }
    ];

    return treeData;
}

/**
 * 只展示一个sourceType的数据源
 * @param viewDiv
 * @param sourceType
 * @param buttons
 */
function showOneTypeDataSource(viewDiv, sourceType, buttons) {
    var treeData = [
        {
            text: '<span style="font-size: 13px;font-weight: 600">' + sourceType + '</span>',
            state: {
                expanded: false
            },
            nodes: getSourceArray(switchReaderTypeToInt(sourceType), buttons)
        }];

    showTreeView(viewDiv, treeData);
}

/**
 * 根据给定treeData在执定区域显示层级列表
 * @param datasourceview
 * @param treeData
 */
function showTreeView(datasourceview, treeData) {
    var treeView = $('#' + datasourceview).treeview({
        expandIcon: 'glyphicon glyphicon-chevron-right',
        collapseIcon: 'glyphicon glyphicon-chevron-down',
        nodeIcon: 'glyphicon glyphicon-bookmark',
        data: treeData
    });
}

/**
 * 根据数据源类型，返回对应的int值
 * @param sourceType
 * @returns {string|*}
 */
function switchReaderTypeToInt(sourceType) {
    sourceType = $.trim(sourceType);
    var sourceTypeInt;
    switch (sourceType) {
        case "mysql":
            sourceTypeInt = 1;
            break;
        case "drds":
            sourceTypeInt = 2;
            break;
        case "oracle":
            sourceTypeInt = 3;
            break;
        case "sqlserver":
            sourceTypeInt = 4;
            break;
        default :
            sourceTypeInt = 255;
    }

    return sourceTypeInt;
}

/**
 * 根据数据源类型获取数据源列表
 * @param type
 * @returns {Array}
 */
function getSourceArray(type, buttons) {
    var findByTypeUrl = getUrlBySuffix("/findByType");
    var params = {};
    params["sourceType"] = type;
    var jdbcAndIds = [];
    $.postHttpSync(params, findByTypeUrl, function (json) {
        jdbcAndIds = extractJdbcAndId(json, buttons);
    });
    return jdbcAndIds;
}

/**
 * 从获取的数据源列表中提取id和jdbcUrl
 * @param json
 * @returns {Array}
 */
function extractJdbcAndId(json, buttons) {
    var jdbcAndIds = [];
    var dataSources = json['list'];


    for (var index in dataSources) {
        var jdbcAndId = {};
        var id = dataSources[index].id;
        var userName = dataSources[index].userName;
        var jdbcUrl = dataSources[index].jdbcUrl;
        var nodeText = assembleNodeText(userName, jdbcUrl);

        jdbcAndId['text'] = "<id class='id' style='display: none'>" + id +
            "</id>" +
            "<span style='font-size: 17px;height:50px;width: 75px;overflow: hidden'>" + nodeText +
            "</span>" +
            "<br>" +
            buttons;

        jdbcAndIds.push(jdbcAndId);
    }
    return jdbcAndIds;
}

/**
 * 组装node的Text
 * @param userName
 * @param jdbcUrl
 * @returns {string}
 */
function assembleNodeText(userName, jdbcUrl) {

    // var nodeText = userName + "@";
    //
    // if (jdbcUrl.contains("DESCRIPTION")) {/*oracle的RAC格式*/
    //     return nodeText + assembleOracleRACNodeText(jdbcUrl);
    // }
    //
    // var removeQuestMark = jdbcUrl.split("?")[0];
    //
    // var ipPortDbReg = /\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}:\d{1,6}(\/|\:)\w+/;
    //
    // var ipPortDb = $.trim(ipPortDbReg.exec(removeQuestMark)).split(",")[0];
    //
    // nodeText += ipPortDb;

    return dataSourceName(userName, jdbcUrl);
}

/**
 *提取oracle的rac格式的jdbc连接
 * @param jdbcUrl
 * @returns {string}
 */
function assembleOracleRACNodeText(jdbcUrl) {
    var ip = $.trim(jdbcUrl.split("HOST")[1].split("=")[1].split(")")[0]);

    var port = $.trim(jdbcUrl.split("PORT")[1].split("=")[1].split(")")[0]);

    var dbName = $.trim(jdbcUrl.split("SERVICE_NAME")[1].split("=")[1].split(")")[0]);

    return ip + ":" + port + "/" + dbName;
}

/**
 * 获取当前操作的数据源的Id
 * @param e
 */
function getCurrentId(ele) {
    var id = ele.parent().parent().find(".id").html();
    return id;
}

function getCurrentDataSourceId(ele) {
    return getCurrentId(ele);
}

/**
 * 获取当前dataSource
 * @param elw
 * @returns {*}
 */
function getCurrentDataSource(ele) {
    var id = ele.parent().parent().find(".id").html();
    var dataSource = getObjectById(getUrlBySuffix("/datasource/findById"), id);
    return dataSource;
}